import mysql.connector

DB_CONFIG = {
    "host": '127.0.0.1',
    "port": 33527,
    "database": "perilla",
    "user": "root",
    "password": "s3cret",
    "charset": "utf8"
}


def test():
    try:
        connection = mysql.connector.connect(pool_name="db_pool", pool_size=3, **DB_CONFIG)
        cursor = connection.cursor()
        sql_query = 'select id, name from foo limit 5'
        cursor.execute(sql_query, ())
        for rid, name in cursor:
            print ('id:%s, name:%s' % (rid, name))
    except mysql.connector.Error as e:
        print('connect fails!{}'.format(e))


def query(sql, args=()):
    try:
        connection = mysql.connector.connect(pool_name="db_pool", pool_size=3, **DB_CONFIG)
        cursor = connection.cursor()
        cursor.execute(sql, args)
        rtn = [dict((cursor.description[i][0], value) for i, value in enumerate(row)) for row in cursor.fetchall()]
        return rtn if rtn else []
    except mysql.connector.Error as e:
        print('connect fails!{}'.format(e))
        return []
